package com.xinxin.leetcode.problem599;

import com.xinxin.leetcode.common.CommonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author ShiXinXin
 * @date 2020-02-09 9:35
 */
public class Solution {
    public String[] findRestaurant(String[] list1, String[] list2) {
        List<String> list = new ArrayList<>();
        int index = 2000;
        for (int i = 0; i < list1.length; i++) {
            for (int j = 0; j < Math.min(list2.length, index - i + 1); j++) {
                if (list1[i].equals(list2[j])) {
                    if (i + j == index) {
                        list.add(list1[i]);
                    } else if (i + j < index) {
                        index = i + j;
                        list.clear();
                        list.add(list1[i]);
                    }
                }
            }
        }
        String[] res = new String[list.size()];
        list.toArray(res);
        return res;
    }

    public static void main(String[] args) {
        String[] list1 = CommonUtil.createArr("Shogun", "Tapioca Express", "Burger King", "KFC");
        String[] list2 = CommonUtil.createArr("Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun");
        System.out.println(Arrays.toString(new Solution().findRestaurant(list1, list2)));
    }
}
